import argparse
import os
from reader import MeshReader, WellReader
from writer import MeshioWriter, WellioWriter

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Process Visual file generation.")
    parser.add_argument("--dir", type=str, required=True, help="Directory path")
    parser.add_argument("--case", type=str, required=True, help="Case name")
    parser.add_argument("--case_path", type=str, default="../data/", help="Output path")
    args = parser.parse_args()

    Mreader = MeshReader()
    Mreader.scan(f"{args.dir}{args.case}")
    print(f"Mesh Info:")
    Mreader.print_grid_info()
    Mreader.print_grid_nums()

    Mwriter = MeshioWriter(Mreader)

    Mwriter.write_xdmf(f"{args.case_path}{args.case}", args.case)

    wellpath = os.path.join(args.dir, f"{args.case}_wstu.out")
    if os.path.exists(wellpath):
        Wreader = WellReader()
        Wreader.scan(f"{args.dir}{args.case}")
        print(f"Well Info:")
        Wreader.print_well_info()
        Wwriter = WellioWriter(Wreader)
        Wwriter.well_offset(Wreader.AxisOffset, Mreader.AxisOffset)
        print(f"Well Axis_offset = {tuple(Wwriter.AxisOffset)}")
        Wwriter.write_xdmf(f"{args.case_path}{args.case}", args.case)
    else:
        print(f"File {wellpath} Not exists.")
